package com.tools.web.interceptor.limit;

import com.tools.common.object.Note;
import com.tools.web.work.ResultCode;

import java.util.Map;
import java.util.Objects;

/**
 * IP-URI 单 IP 映射单接口的请求限流拦截器所需的配置项参数包装类
 * */
@Note("IP-URI 单 IP 映射单接口的请求限流拦截器所需的配置项参数包装类")
public class RequestLimitSource {

    @Note("缓存 key 的前缀")
    private String saveKeyPrefix;

    @Note("请求被限流后的错误信息")
    private String limitErrorMessage;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public RequestLimitSource() {
        this.saveKeyPrefix = "limit:req:";
        this.limitErrorMessage = ResultCode.ACCESS_LIMIT.toJSON();
    }

    public static RequestLimitSource of() {
        return new RequestLimitSource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static RequestLimitSource from(Map properties) {
        RequestLimitSource source = new RequestLimitSource();
        if(properties == null || properties.isEmpty()) return source;
        source.setSaveKeyPrefix((String) properties.get("limit-interceptor-saveKeyPrefix"));
        source.setLimitErrorMessage((String) properties.get("limit-interceptor-limitErrorMessage"));
        return source;
    }

    /* **************************************************************************************
     *
     *          Getter / Setter
     *
     * **************************************************************************************
     * */

    public String getSaveKeyPrefix() {
        return saveKeyPrefix;
    }

    public void setSaveKeyPrefix(String saveKeyPrefix) {
        if(saveKeyPrefix != null && !saveKeyPrefix.isEmpty()) {
            this.saveKeyPrefix = saveKeyPrefix;
        }
    }

    public String getLimitErrorMessage() {
        return limitErrorMessage;
    }

    public void setLimitErrorMessage(String limitErrorMessage) {
        if(limitErrorMessage != null && !limitErrorMessage.isEmpty()) {
            this.limitErrorMessage = limitErrorMessage;
        }
    }

    @Override
    public String toString() {
        return "RequestLimitSource{" +
                "saveKeyPrefix=" + saveKeyPrefix +
                ", limitErrorMessage=" + limitErrorMessage +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RequestLimitSource that = (RequestLimitSource) o;
        return Objects.equals(saveKeyPrefix, that.saveKeyPrefix) && Objects.equals(limitErrorMessage, that.limitErrorMessage);
    }

    @Override
    public int hashCode() {
        return Objects.hash(saveKeyPrefix, limitErrorMessage);
    }

    /* **************************************************************************************
     *
     *          链式调用
     *
     * **************************************************************************************
     * */

    public RequestLimitSource saveKeyPrefix(String saveKeyPrefix) {
        this.setSaveKeyPrefix(saveKeyPrefix);
        return this;
    }

    public RequestLimitSource limitErrorMessage(String limitErrorMessage) {
        this.setLimitErrorMessage(limitErrorMessage);
        return this;
    }
}
